package solucionessgh.Usuarios;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import solucionessgh.Util.DataBase;

/**
 *
 * @author roderick
 */
public class UsuariosModel extends DataBase{

    private int id;
    private String usulogin;
    private String usupass;
    private String usunom;
    private int nuscod;



    public UsuariosModel(){

    }

    private boolean validar(){
        /*if(this.usulogin.length()!=1){
            JOptionPane.showMessageDialog(null,
            "Codigo debe tener un digito",
            "Nivel Usuario",
            JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if(this.usupass.length()>20){
            JOptionPane.showMessageDialog(null,
            "Descripcion menor a 20 digito",
            "Nivel Usuario",
            JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if(this.usunom.length()!=3){
            JOptionPane.showMessageDialog(null,
            "Codigo debe tener tres digito",
            "Nivel Usuario",
            JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if(this.usulogin.length()!=3){
            JOptionPane.showMessageDialog(null,
            "Codigo debe tener tres digito",
            "Nivel Usuario",
            JOptionPane.ERROR_MESSAGE);
            return false;
        }*/
        return true;

    }

    public boolean create(){
        if(validar()){            
            String url = "jdbc:postgresql://localhost/sgh";
            try{
                Class.forName("org.postgresql.Driver");
                Connection con = DriverManager.getConnection( url,"postgres","postgres");
                PreparedStatement stmt = con.prepareStatement("INSERT INTO usuario(usulogin,usupass,usunom,nuscod) VALUES (?,?,?,?)");
                stmt.setString(1,this.usulogin);
                stmt.setString(2,this.usupass);
                stmt.setString(3,this.usunom);
                stmt.setInt(4,this.nuscod);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
            return false;            
        }           
        return false;
    }

    public boolean update(){
        if(validar()){
            String url = "jdbc:postgresql://localhost/sgh";
            try{
                Class.forName("org.postgresql.Driver");
                Connection con = DriverManager.getConnection( url,"postgres","postgres");
                PreparedStatement stmt = con.prepareStatement("update usuario set usulogin = ? , usupass = ? , usunom = ? , nuscod = ? where usuid = ?;");
                stmt.setString(1,this.usulogin);
                stmt.setString(2,this.usupass);
                stmt.setString(3,this.usunom);
                stmt.setInt(4,this.nuscod);
                stmt.setInt(5, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
            return false;
        }
        return false;
    }

    public boolean delete(){
        if(validar()){
            String url = "jdbc:postgresql://localhost/sgh";
            try{
                Class.forName("org.postgresql.Driver");
                Connection con = DriverManager.getConnection( url,"postgres","postgres");
                PreparedStatement stmt = con.prepareStatement("delete from usuario where usuid=?;");
                stmt.setInt(1, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
            return false;
        }
        return false;
    }

    public ResultSet read(){
        return this.find("SELECT u.usulogin,u.usupass,u.usunom,u.id,nu.nusdesc as nuscod  FROM usuario u  INNER JOIN nivelusuario nu ON  u.nuscod = nu.nuscod");
    }

    public ResultSet finder(String var){
        return this.find("SELECT usulogin, usupass, usunom, nu.nusdesc as nuscod, usuid "+
                         "FROM usuario as u "+
                         "INNER JOIN nivelusuario as nu ON "+
                         "u.nuscod = nu.nuscod "+
                         "WHERE u.usunom LIKE '"+var+"%';");
    }

    public ResultSet finder(int var){       
        return this.find("SELECT usulogin, usupass, usunom, nuscod, usuid FROM usuario WHERE usuid = "+var+";");
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getNuscod() {
        return nuscod;
    }

    public void setNuscod(int nuscod) {
        this.nuscod = nuscod;
    }

    public String getUsulogin() {
        return usulogin;
    }

    public void setUsulogin(String usulogin) {
        this.usulogin = usulogin;
    }

    public String getUsunom() {
        return usunom;
    }

    public void setUsunom(String usunom) {
        this.usunom = usunom;
    }

    public String getUsupass() {
        return usupass;
    }

    public void setUsupass(String usupass) {
        this.usupass = usupass;
    }

    public boolean dologin(){
        String url = "jdbc:postgresql://localhost/sgh";
        ResultSet rs = null;
        try{
            Class.forName("org.postgresql.Driver");
            Connection con = DriverManager.getConnection( url,"postgres","postgres");
            Statement stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT usunom FROM usuario WHERE usulogin ='"+this.usulogin+"' and usupass = '"+this.usupass+"'");
            while ( rs.next()){
                JOptionPane.showMessageDialog(null,
                "Bienvenido "+rs.getString("usunom"),
                "Nivel Usuario",
                JOptionPane.INFORMATION_MESSAGE);
                return true;
            }
        }
        catch( Exception e ){
            
        }
        return false;
    }
}
